package com.example.emos.wx.user.service.impl;


import com.example.emos.wx.checkin.model.pojo.TbDept;
import com.example.emos.wx.core.exception.EmosException;
import com.example.emos.wx.user.db.dao.TbDeptDao;
import com.example.emos.wx.user.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private TbDeptDao deptDao;

    @Override
    public List<TbDept> searchAllDept() {
        return deptDao.searchAllDept();
    }

    @Override
    public void insertDept(String deptName) {
        int row = deptDao.insertDept(deptName);
        if (row != 1) {
            throw new EmosException("添加部门失败");
        }
    }

    @Override
    public void deleteDeptById(int id) {
        long count = deptDao.searchUserCountInDept(id);
        if (count > 0) {
            throw new EmosException("该部门关联员工，无法删除");
        } else {
            int row = deptDao.deleteDeptById(id);
            if (row != 1) {
                throw new EmosException("部门删除失败");
            }
        }
    }

    @Override
    public void updateDeptById(TbDept dept) {
        int row = deptDao.updateDeptById(dept);
        if (row != 1) {
            throw new EmosException("修改部门失败");
        }
    }
}
